<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Smarty price modifier plugin
 *
 * Type:     modifier<br>
 * Name:     price<br>
 * Purpose:  Substr a string to a certain length if necessary,
 *           optionally splitting in the middle of a word, and
 *           appending the $etc string or inserting $etc into the middle.
 * @link http://smarty.php.net/manual/en/language.modifier.price.php
 *          price (Smarty online manual)
 * @author   Monte Ohrt <monte at ohrt dot com>
 * @param string
 * @param integer
 * @param string
 * @param boolean
 * @param boolean
 * @return string
 */
function smarty_modifier_price($string, $format = '', $dsct = '')
{
	if( $format == 'space' && ($string == '' || $string == '0' || $string == '0.00' || $string == '0.0000') ) {
		return '';
	}
	if( $dsct == 'discount' ) {
		if( CONST_PRICE_MODE_TYPE == CONST_PRICE_MODE_UCT ) {
			global $ppmuctObj;
			
			$product = '';
			if( is_string($string) && strstr($string, '|') !== false ) {
				$ss = explode('|', $string);
				$product = array(
						'products_price' => $ss['0'],
						'products_price1' => $ss['1'],
						'products_price2' => $ss['2'],
						'products_price3' => $ss['3'],
						'products_price4' => $ss['4'],
						'products_price5' => $ss['5'],
						'categories_id' => $ss['6'],
							);
			} elseif( is_array($string) ) {
				$product = $string;	
			}
			if( !is_object($ppmuctObj) ) {
				include_once(CONFIG_PATH . CONFIG_DIR_CLASSES . 'product.price.model.uct.class.php');
				$ppmuctObj = new ProductPriceModelUCTBase();
			}
			if( is_array($product) ) {//product data
				$ppmuctObj->setProduct($product);
				
				$string = $ppmuctObj->getProductPrice();
			}
		} else {
			if( $_SESSION['suser']->userid > 0 && $_SESSION['suser']->discount > 0 ) {
				$string = floatval($string) * (1-$_SESSION['suser']->discount);
			}
		}
	}
	//
	if( $format == 'value' ) {
		$price = round(floatval($string), 2);
		if( $price == '' ) {
			$price = 0;
		}
		return $prefix . $price;
	} elseif( $format == 'number' ) {
		$price = mxx_price_format($string);
		if( $price == '' ) {
			$price = 0;	
		}
		return $prefix . $price;
	} elseif( $format == 'cart' ) {
		$prefix = '';
		if( $string > 0 ) {
			$price = mxx_price_format(abs($string));
		} elseif( $string == 0 || $string == '' ) {
			$price = '0';
		} elseif( $string < 0 ) {
			$prefix = '-';
			$price = mxx_price_format(abs($string));
		}
		return $prefix . '$' . $price;
	} else {
		$prefix = ($string >= 0) ? '' : '- ';
		$price = mxx_price_format(abs($string));
		if( $price == '' ) {
			return '0';
		} else {
			return $prefix . '$' . $price;
		}
	}
}

function mxx_price_format($value, $precision = 2) {
	if( strpos($value, '.') !== false ) {
		//$value = substr($value, 0, strpos($value, '.') + 1 + $precision + 1);
		//$value = number_format($value, 2, '.', '');
		$value = number_format($value, $precision);
	} else {
		$value = number_format($value, $precision);
	}
	
	return $value;
}
//
?>